//
//  URLRouterMap.swift
//  Nahuo
//
//  Created by zang qilong on 2017/5/12.
//  Copyright © 2017年 zang qilong. All rights reserved.
//

import Foundation
import URLNavigator

struct URLNavigationMap {
    static func initialize() {
        Navigator.scheme = "nahuo"
        
        // tab Home Page
        Navigator.map("/market", MarketViewController.self)
        Navigator.map("/myfavourite", MyFavouriteViewController.self)
        Navigator.map("/classify", NahuoCategoryViewController.self)
        Navigator.map("/self", SelfTableViewController.self)
        
        // Web
        Navigator.map("http://<path:_>", WebViewController.self)
        Navigator.map("https://<path:_>", WebViewController.self)
        Navigator.map("navigator://alert", self.alert)
        
        // Market 
        Navigator.map("/detail/<id>", ProductDetailViewController.self)
        
        // MyFavourite
        
        // Classify
        
        // Self
        
        // Login 
        Navigator.map("/verifyCode/<phone>") { (url, values) -> Bool in
            guard let phone = values["phone"] as? String ,
                let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "kVerifyCodeVC") as? VerifyCodeViewController else {
                return false
            }
            vc.phoneNum = phone
            Navigator.push(vc)
            return true
        }
        
        Navigator.map("/verifyPassword/<phone>") { (url, values) -> Bool in
            guard let phone = values["phone"] as? String ,
                let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "kEnterPasswordVC") as? EnterPasswordViewController else {
                    return false
            }
            vc.phoneNum = phone
            Navigator.push(vc)
            return true
        }
    }
    
    private static func alert(URL: URLConvertible, values: [String: Any]) -> Bool {
        let title = URL.queryParameters["title"]
        let message = URL.queryParameters["message"]
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        Navigator.present(alertController)
        return true
    }
}
